package com.cy.Service.ServiceImpl;

import Pojo.Node;
import com.cy.Dao.SysMenuDao;
import com.cy.Service.SysMenusService;
import com.cy.pojo.SysMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = false,
               rollbackFor = Throwable.class,
               isolation = Isolation.READ_COMMITTED)
@Slf4j
@Service
public class SysMenusServiceImpl implements SysMenusService {
    @Autowired
    private SysMenuDao sysMenuDao;
    @Transactional(readOnly = true)
    @Override
    public List<SysMenu> selectMenus() {
      /**第一种方式*/
         return sysMenuDao.selectMenus();
        /**第二种方式(拓展思考）
         * 查找所有信息菜单select * from sys_menus
         * 在业务层对数据进行封装（一级菜单，二级菜单，三级菜单，......)递归算法
         * 第一种方式是在数据层磁盘里操作，业务层实在内存中操作，性能高
         * List<SysMenu> menus=sysMenuDao.selectMenus();
         * menus.stream().filter().collect()
         * */

    }

    @Transactional(readOnly = true)
    @Override
    public List<Node> selectMenuTreeNodes() {
        return sysMenuDao.selectMenuTreeNodes();
    }

   @Override
    public int insertMenu(SysMenu Menu) {
        return sysMenuDao.insertMenu(Menu);
    }

    @Transactional(readOnly = true)
    @Override
    public SysMenu selectById(Integer id) {
        SysMenu menu=sysMenuDao.selectById(id);
        return menu;
    }

    @Override
    public int updateMenu(SysMenu menu) {
        if(menu.getId()==null){
            return sysMenuDao.insertMenu(menu);
        }
        return sysMenuDao.updateMenu(menu);
    }
}
